home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpz_mod_2exp.c < prev    next >
C/C++ Source or Header  |  1993-05-02  |  2KB  |  83 lines

  1. /* mpz_mod_2exp -- divide a MP_INT by 2**n and produce a remainder.
  2.  
  3. Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with the GNU MP Library; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "gmp.h"
  22. #include "gmp-impl.h"
  23.  
  24. void
  25. #ifdef __STDC__
  26. mpz_mod_2exp (MP_INT *res, const MP_INT *in, unsigned long int cnt)
  27. #else
  28. mpz_mod_2exp (res, in, cnt)
  29.      MP_INT *res;
  30.      const MP_INT *in;
  31.      unsigned long int cnt;
  32. #endif
  33. {
  34.   mp_size in_size = ABS (in->size);
  35.   mp_size res_size;
  36.   mp_size limb_cnt = cnt / BITS_PER_MP_LIMB;
  37.   mp_srcptr in_ptr = in->d;
  38.  
  39.   if (in_size > limb_cnt)
  40.     {
  41.       /* The input operand is (probably) greater than 2**CNT.  */
  42.       mp_limb x;
  43.  
  44.       x = in_ptr[limb_cnt] & (((mp_limb) 1 << cnt % BITS_PER_MP_LIMB) - 1);
  45.       if (x != 0)
  46.     {
  47.       res_size = limb_cnt + 1;
  48.       if (res->alloc < res_size)
  49.         _mpz_realloc (res, res_size);
  50.  
  51.       res->d[limb_cnt] = x;
  52.     }
  53.       else
  54.     {
  55.       mp_size i;
  56.  
  57.       for (i = limb_cnt - 1; i >= 0; i--)
  58.         if (in_ptr[i] != 0)
  59.           break;
  60.       res_size = i + 1;
  61.  
  62.       if (res->alloc < res_size)
  63.         _mpz_realloc (res, res_size);
  64.  
  65.       limb_cnt = res_size;
  66.     }
  67.     }
  68.   else
  69.     {
  70.       /* The input operand is smaller than 2**CNT.  We perform a no-op,
  71.      apart from that we might need to copy IN to RES.  */
  72.       res_size = in_size;
  73.       if (res->alloc < res_size)
  74.     _mpz_realloc (res, res_size);
  75.  
  76.       limb_cnt = res_size;
  77.     }
  78.  
  79.   if (res != in)
  80.     MPN_COPY (res->d, in->d, limb_cnt);
  81.   res->size = (in->size >= 0) ? res_size : -res_size;
  82. }
  83.